package vigo_tool

import (
	"crypto/md5"
	"encoding/hex"
	"errors"
	"regexp"
)

const (
	PHONE = 1
	EMAIL = 2
)

// 是否是手机或邮箱
func IsPhoneOrEmail(sendTool string) (int, error) {
	match, err := regexp.MatchString("^1[0-9]{10}$", sendTool)
	if err != nil {
		return 0, err
	}
	if match {
		return PHONE, nil
	}
	match, err = regexp.MatchString(`^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$`, sendTool)
	if err != nil {
		return 0, err
	}
	if match {
		return EMAIL, nil
	}
	return 0, errors.New("param fail")
}

// 生成密码
func GetPassword(password, salt string) string {
	h := md5.New()
	h.Write([]byte(password + salt))
	return hex.EncodeToString(h.Sum(nil))
}

// 验证密码格式
func VerifyPwd(passwd string) bool {
	match, _ := regexp.MatchString("[0-9]{1,30}", passwd)

	if !match {
		return match
	}

	match, _ = regexp.MatchString("[a-zA-Z]{1,30}", passwd)

	return match
}
